<?php

namespace App\Http\Controllers;

use App\Models\ConfigModel;
use App\Repositories\ConfigRepository;
use Illuminate\Http\Request;

class ConfigController extends BaseController
{

    public function getMarketIndexOptions (ConfigRepository $configRepository)
    {
        $a = [['value' => 10, 'label' => '上涨中'], ['value' => 20, 'label' => '上涨回调'], ['value' => 30, 'label' => '横盘上涨'], ['value' => 40, 'label' => '横盘'], ['value' => 50, 'label' => '横盘下跌'], ['value' => 60, 'label' => '下跌'], ['value' => 70, 'label' => '下跌中回调'] ];
        return $this->success($a);
//        $sseCompositeIndex = @$configRepository->getConfigByKeyword('market_index', 'json_decode');
//        return $this->success($sseCompositeIndex);
    }

    /**
     * 获取配置信息
     * @param ConfigRepository $configRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function info (ConfigRepository $configRepository)
    {
        $configKeys = [
            'initial_money',
            'enter_market_time'
        ];
        $config = @$configRepository->getConfigByKeywords($configKeys);
        if ($config) {
            return $this->success($config);
        }
        return $this->error(1001, '数据获取错误');
    }

    /**
     * 保存配置信息
     * @param Request $request
     * @param ConfigRepository $configRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function save (Request $request, ConfigRepository $configRepository)
    {
        $allParams = $request->all();
        $rst = @$configRepository->updateConfigByKeywords($allParams);
        if ($rst) {
            return $this->success();
        } else {
            return $this->error(1001, '保存失败');
        }
    }
}
